package org.example.topicdata;

import org.example.ConnectDatabase;
import org.example.Server;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DeleteTopic extends ConnectDatabase{

    int isOK = 1;
    public int deleteTopic(Topic topic){
        new ConnectDatabase();
        try {
            System.out.println("sss"+topic.ID);
            String SQL ="delete from Topics where TID = " + topic.ID + "";
            PreparedStatement sta  = con.prepareStatement(SQL);
            isOK = sta.executeUpdate();
            for (int i = 0; i < Server.topicarrayList.size(); i++) {
                if (Server.topicarrayList.get(i).ID == topic.ID){
                    Server.topicarrayList.remove(i);
                    break;
                }
            }
            con.close();
        }
        catch(SQLException e) {
            isOK = 0;                //Topics表中ID字段是主键，即不允许ID重复
        }
        return isOK;
    }
}
